<template>
  <div class="mtype-container">
    <van-swipe
      :loop="false"
      :autoplay="1113000"
      class="mtype-swipe"
      @change="onChange"
      v-if="types.length>0"
    >
      <van-swipe-item v-for="(item, index) in types" :key="index">
        <div class="mtype-swipe-item">
          <van-cell :title="item.name" :label="item.description" :border="false">
            <van-icon slot="right-icon" :name="item.icon" />
          </van-cell>
          <van-button round type="info" size="small" @click="onApply">立即申请</van-button>
        </div>
      </van-swipe-item>
    </van-swipe>
    <div class="mtype-notice">
      <van-cell title="马上成为优越会员" icon="fire" value="申请流程" />
    </div>
    <div class="mtype-privilege" v-if="currentType && privileges.length>0">
      <h1>
        {{currentType.name}}
        <small>尊享{{privileges.length}}项特权</small>
      </h1>
      <van-grid class="mtype-privilege-list" :border="false">
        <van-grid-item
          :icon="item.icon?item.icon:'diamond-o'"
          :text="item.name"
          v-for="(item, index) in privileges"
          :key="index"
        />
      </van-grid>
    </div>
    <div class="mtype-action">
      <van-button v-if="currentType" size="large" block @click="onApply">申请成为{{currentType.name}}会员</van-button>
    </div>
  </div>
</template>

<script>
import {
  Row,
  Col,
  Cell,
  Button,
  CellGroup,
  Swipe,
  SwipeItem,
  Icon,
  Image,
  Grid,
  GridItem,
  Lazyload
} from 'vant'

export default {
  components: {
    [Row.name]: Row,
    [Col.name]: Col,
    [Cell.name]: Cell,
    [Button.name]: Button,
    [CellGroup.name]: CellGroup,
    [Swipe.name]: Swipe,
    [SwipeItem.name]: SwipeItem,
    [Icon.name]: Icon,
    [Image.name]: Image,
    [Grid.name]: Grid,
    [GridItem.name]: GridItem,
    Lazyload
  },

  data () {
    return {
      current: 0,
      currentId: 0,
      types: [],
      privileges: []
    }
  },

  created () {
    this.onLoad()
  },
  watch: {
    // currentType: {
    //   handler (cv, ov) {
    //     this.currentId = cv.id
    //   },
    //   deep: true
    // }
  },
  computed: {
    currentType () {
      let node = this.types[this.current]
      if (node) {
        this.getPrivileges(node.id)
      }
      return node
    }
  },

  methods: {
    onChange (index) {
      this.current = index
    },
    onLoad () {
      this.axios
        .get('/member/type')
        .then(response => {
          this.types = response.data
        })
        .catch(function (error) {
          console.log(error)
        })
    },
    onApply () {
      this.pushNativelink(this.currentType.link)
    },
    getPrivileges (id) {
      this.axios
        .get('/member/privilege', {
          params: { id: id },
          headers: { FastMode: true }
        })
        .then(response => {
          this.privileges = response.data
        })
        .catch(function (error) {
          console.log(error)
        })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.mtype-container {
  position: relative;
  overflow: hidden;
  width: 100%;
  min-height: 100%;
  &::before {
    position: absolute;
    left: -25%;
    top: -3px;
    width: 150%;
    height: 200px;
    background: #e3c184;
    border-radius: 0 0 100% 100%;
    content: "";
    border: 3px dashed rgba(255, 255, 255, 0.3);
    display: block;
  }
}
.mtype-action {
  padding: 15px;
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;

  .van-button--default {
    background-color: #e3c184;
    border-color: #e3c184;
    color: #805916;
    border-radius: 5px;
  }
}
.mtype-swipe {
  margin: 0 0 15px;
  &-item {
    position: relative;
    padding: 20px;
    border-radius: 10px;
    overflow: hidden;
    background: rgb(214, 235, 254);
    background: -moz-linear-gradient(
      -45deg,
      rgba(214, 235, 254, 1) 0%,
      rgba(197, 228, 255, 1) 50%,
      rgba(179, 222, 255, 1) 100%
    );
    background: -webkit-linear-gradient(
      -45deg,
      rgba(214, 235, 254, 1) 0%,
      rgba(197, 228, 255, 1) 50%,
      rgba(179, 222, 255, 1) 100%
    );
    background: linear-gradient(
      135deg,
      rgba(214, 235, 254, 1) 0%,
      rgba(197, 228, 255, 1) 50%,
      rgba(179, 222, 255, 1) 100%
    );
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#d6ebfe', endColorstr='#b3deff',GradientType=1 );

    &__bg {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
    }
    .van-cell {
      background: transparent;
      padding-left: 0;
      padding-right: 0;
      &__title {
        font-size: 16px;
        font-weight: 600;
        color: rgba(70, 135, 240, 1);
      }
      &__label {
        font-weight: 300;
        color: rgba(70, 135, 240, 0.5);
      }
      .van-icon {
        font-size: 50px;
      }
    }
  }
  /deep/ .van-swipe {
    &__track {
      height: 180px;
    }
    &-item {
      padding: 15px;
      &:nth-child(2) {
        .mtype-swipe-item {
          background: rgb(245, 223, 186);
          background: -moz-linear-gradient(
            -45deg,
            rgba(245, 223, 186, 1) 0%,
            rgba(236, 208, 161, 1) 50%,
            rgba(230, 195, 139, 1) 100%
          );
          background: -webkit-linear-gradient(
            -45deg,
            rgba(245, 223, 186, 1) 0%,
            rgba(236, 208, 161, 1) 50%,
            rgba(230, 195, 139, 1) 100%
          );
          background: linear-gradient(
            135deg,
            rgba(245, 223, 186, 1) 0%,
            rgba(236, 208, 161, 1) 50%,
            rgba(230, 195, 139, 1) 100%
          );
          filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f5dfba', endColorstr='#e6c38b',GradientType=1 );

          .van-cell {
            &__title {
              color: rgba(117, 76, 8, 1);
            }
            &__label {
              color: rgba(117, 76, 8, 0.5);
            }
          }
          .van-button--info {
            background-color: rgba(117, 76, 8, 1);
            border-color: rgba(117, 76, 8, 1);
          }
        }
      }
      &:nth-child(3) {
        .mtype-swipe-item {
          background: rgb(57, 58, 62);
          background: -moz-linear-gradient(
            -45deg,
            rgba(57, 58, 62, 1) 0%,
            rgba(51, 50, 55, 1) 50%,
            rgba(42, 41, 46, 1) 100%
          );
          background: -webkit-linear-gradient(
            -45deg,
            rgba(57, 58, 62, 1) 0%,
            rgba(51, 50, 55, 1) 50%,
            rgba(42, 41, 46, 1) 100%
          );
          background: linear-gradient(
            135deg,
            rgba(57, 58, 62, 1) 0%,
            rgba(51, 50, 55, 1) 50%,
            rgba(42, 41, 46, 1) 100%
          );
          filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#393a3e', endColorstr='#2a292e',GradientType=1 );

          .van-cell {
            &__title {
              color: rgba(255, 255, 255, 1);
            }
            &__label {
              color: rgba(255, 255, 255, 0.5);
            }
          }
          .van-button--info {
            background-color: #e3c184;
            border-color: #e3c184;
            color: #7d530b;
          }
        }
      }
    }
  }
}

.mtype-notice {
  padding: 20px 15px;
  .van-cell {
    border-radius: 30px;
    background: #fcfaed;
    padding: 5px 10px;
    font-size: 12px;
    line-height: 24px;
    &__left-icon {
      color: #ff5544;
    }
    &__right-icon {
      color: #f1b249;
    }
    &__title {
      color: #6d511f;
    }
    &__value {
      color: #f1b249;
    }
  }
}
.mtype-privilege {
  h1 {
    text-align: center;
    font-weight: 600;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #7d5609;
    small {
      font-weight: normal;
      opacity: 0.9;
    }
    &::before,
    &::after {
      content: "";
      width: 3px;
      height: 10px;
      background: #dfc087;
      margin: 0 10px;
      border-radius: 3px;
      transform: rotate(30deg);
    }
  }
  &-list {
    /deep/ .van-grid-item__content {
      background: transparent;
    }
  }
}
</style>
